/*
 * @Author: your name
 * @Date: 2021-07-19 11:08:02
 * @LastEditTime: 2021-07-23 17:15:00
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \cuiyuan_activity\assets\script\Video.ts
 */
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Game from "./Game";
import { getStore, setStore } from "./Utils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Begin extends cc.Component {
    static inst: Begin;
    // 视频组件
    btn: cc.Node = null;
    door: cc.Node = null;
    bg: cc.Node = null;
    ticket_btn: cc.Node = null;

    onLoad() {
        Begin.inst = this;
        this.init();
    }

    // 初始化场景
    init() {
        this.btn = cc.find('begin', this.node)
        this.door = cc.find('door', this.node)
        this.bg = cc.find('bg', this.node)
        this.ticket_btn = cc.find('ticket_btn', this.node)
        let getTicket = getStore('getTicket', false)
        if (getTicket) {
            this.ticket_btn.active = true;
        }
    }

    // 开场播放完毕
    handleDoor() {
        Game.inst.playMainBgm();
        this.btn.active = false;
        this.ticket_btn.active = false;
        cc.tween(this.bg)
            .to(2, { scale: 2 })
            .call(() => {
                this.door.active = true;
                this.bg.active = false;
                this.openDoor();
                Game.inst.nextScene('StartScene')
            })
            .start()
    }

    // 打开船票场景
    openTicketScene() {
        this.hide();
        Game.inst.nextScene('TicketScene')
    }

    // 开门动画
    openDoor() {

        // 获取两个动画集 并播放 播放完毕的时候，让节点消失
        let leftAnimation = cc.find('left', this.door).getComponent(cc.Animation);
        let rightAnimation = cc.find('right', this.door).getComponent(cc.Animation);
        leftAnimation.play();
        rightAnimation.play();
        leftAnimation.on('finished', this.hide, this);
    }

    show() {
        this.node.active = true;
    }

    hide() {
        this.node.active = false;
    }

    // update (dt) {}
}
